package com.oa.ylms.apps.business.assess.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.oa.ylms.apps.business.archives.IStaffInfoService;
import com.oa.ylms.apps.business.assess.IAssessDetailInfoService;
import com.oa.ylms.apps.business.assess.IAssessInfoService;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.constants.assess.IAssessInfoStates;
import com.oa.ylms.apps.dao.assess.IAssessDetailInfoDao;
import com.oa.ylms.apps.entity.archives.ArchiveInfoValue;
import com.oa.ylms.apps.entity.assess.AssessDetailInfoValue;
import com.oa.ylms.apps.entity.assess.AssessInfoValue;

public class AssessDetailInfoServiceImpl implements IAssessDetailInfoService {

	private AssessDetailInfoValue assessDetailInfo;
	private IAssessInfoService assessInfoService;
	private IStaffInfoService staffInfoService;
	private ArchiveInfoValue archiveInfo;
	private AssessInfoValue assessInfo;
	private IAssessDetailInfoDao dao;

	public int addAssessDetailInfo(int assessId, String staffIds,
			int scoresCount) throws Exception {
		assessDetailInfo.setAssessInfo(assessInfoService
				.queryAssessInfoByAssessId(assessId));
		String assessScores = "0";
		for (int i = 1; i < scoresCount; i++) {
			assessScores += ",0";
		}
		assessDetailInfo.setAssessScores(assessScores);
		String[] arrayStaffId = staffIds.split(",");
		for (String staffIdStr : arrayStaffId) {
			int staffId = Integer.parseInt(staffIdStr);
			assessDetailInfo.setStaffInfo(staffInfoService
					.queryByStaffId(staffId));
			dao.addAssessDetailInfo(assessDetailInfo);
		}
		return 1;
	}

	public String deleteAssessDetailInfo(String detailIds) throws Exception {
		String unDelIds = "";
		String[] arrayDetailId = detailIds.split(",");
		for (String detailIdStr : arrayDetailId) {
			int detailId = Integer.parseInt(detailIdStr);
			assessDetailInfo = queryAssessDetailInfoByDetailId(detailId);
			boolean result = dao.deleteAssessDetailInfo(assessDetailInfo);
			if (result == false) {
				unDelIds = detailId + ",";
				continue;
			}
		}
		return unDelIds;
	}

	public List<AssessDetailInfoValue> queryAllAssessDetailInfo()
			throws Exception {

		return dao.queryAllAssessDetailInfo();
	}

	public AssessDetailInfoValue queryAssessDetailInfoByDetailId(int detailId)
			throws Exception {

		return dao.queryAssessDetaiInfoByDetailId(detailId);
	}

	public AssessDetailInfoValue getAssessDetailInfo() {
		return assessDetailInfo;
	}

	public void setAssessDetailInfo(AssessDetailInfoValue assessDetailInfo) {
		this.assessDetailInfo = assessDetailInfo;
	}

	public IAssessInfoService getAssessInfoService() {
		return assessInfoService;
	}

	public void setAssessInfoService(IAssessInfoService assessInfoService) {
		this.assessInfoService = assessInfoService;
	}

	public IStaffInfoService getStaffInfoService() {
		return staffInfoService;
	}

	public void setStaffInfoService(IStaffInfoService staffInfoService) {
		this.staffInfoService = staffInfoService;
	}

	public IAssessDetailInfoDao getDao() {
		return dao;
	}

	public void setDao(IAssessDetailInfoDao dao) {
		this.dao = dao;
	}

	public int countTpages(String staffName, int assessId, int rows)
			throws Exception {
		if (StringUtils.isNotEmpty(staffName)
				&& StringUtils.isNotBlank(staffName)) {
			archiveInfo.setStaffName(staffName);
			archiveInfo.setStates(IDEDStates.ENABLE);
		}
		assessInfo = assessInfoService.queryAssessInfoByAssessId(assessId);
		assessInfo.setStates(IAssessInfoStates.UNDEFINED);
		return dao.countTpages(archiveInfo, assessInfo, rows);
	}

	public int getTotalNumber() throws Exception {

		return dao.getTotalNumber();
	}

	public List<AssessDetailInfoValue> queryAssessDetailInfoWithPageByAssessId(
			String staffName, int assessId, int rows, int pageNo)
			throws Exception {
		if (StringUtils.isNotEmpty(staffName)
				&& StringUtils.isNotBlank(staffName)) {
			archiveInfo.setStaffName(staffName);
			archiveInfo.setStates(IDEDStates.ENABLE);
		}
		assessInfo = assessInfoService.queryAssessInfoByAssessId(assessId);
		assessInfo.setStates(IAssessInfoStates.UNDEFINED);
		return dao.queryAssessDetailInfoWithPageByAssessId(archiveInfo,
				assessInfo, rows, pageNo);
	}

	public boolean modifyAssessDetailInfo(int detailId, String assessScores)
			throws Exception {
		assessDetailInfo = queryAssessDetailInfoByDetailId(detailId);
		assessDetailInfo.setAssessScores(assessScores);
		return dao.modifyAssessDetailInfo(assessDetailInfo);
	}

	public ArchiveInfoValue getArchiveInfo() {
		return archiveInfo;
	}

	public void setArchiveInfo(ArchiveInfoValue archiveInfo) {
		this.archiveInfo = archiveInfo;
	}

	public AssessInfoValue getAssessInfo() {
		return assessInfo;
	}

	public void setAssessInfo(AssessInfoValue assessInfo) {
		this.assessInfo = assessInfo;
	}

}
